package com.crazypos.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crazypos.common.ApiRestResponse;
import com.crazypos.common.utils.SecurityUtil;
import com.crazypos.pojo.CustomerEntity;
import com.crazypos.pojo.bo.CreateCustomerBO;
import com.crazypos.security.entity.UserDetailEntity;
import com.crazypos.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @GetMapping("/list")
    @PreAuthorize("hasRole('ADMIN') or hasPermission('/customer/list','SYS:QUERY_CUSTOMER')")
    public ApiRestResponse getCusomtersList(
            @DefaultValue("1") Integer startPage,
            @DefaultValue("10") Integer pageSize,
            @DefaultValue("") String keyword
    ) {
        String organizationId = SecurityUtil.getOrganizationId();
        IPage<CustomerEntity> result = customerService.getCusomtersList(startPage, pageSize, keyword, organizationId);
        return ApiRestResponse.success(result);
    }

    @GetMapping("/update")
    @PreAuthorize("hasRole('ADMIN') or hasPermission('/customer/update','SYS:UPDATE_CUSTOMER')")
    public ApiRestResponse updateCustomer() {
        return ApiRestResponse.success();
    }

    @GetMapping("/create")
    @PreAuthorize("hasRole('ADMIN') or hasPermission('/customer/create','SYS:CREATE_CUSTOMER')")
    public ApiRestResponse createCustomer(@RequestBody @Validated CreateCustomerBO bo) {
        UserDetailEntity userInfo = SecurityUtil.getUserInfo();
        customerService.createCustomer(bo,userInfo.getId(), userInfo.getOrganizationId());
        return ApiRestResponse.success();
    }

    @GetMapping("/delete")
    @PreAuthorize("hasRole('ADMIN') or hasPermission('SYS:DELETE_CUSTOMER')")
    public ApiRestResponse deleteCustomer() {
        return ApiRestResponse.success();
    }

}
